package org.darkforest.common.services.impl;

import org.darkforest.common.services.VoucherService;
import org.darkforest.common.services.VoucherTemplateService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.SerialNumGenerator;
import org.darkforest.dao.mapper.VoucherMapper;
import org.darkforest.model.Voucher;
import org.darkforest.model.VoucherSearchBean;
import org.darkforest.model.VoucherTpl;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/5.
 */
@Service
public class VoucherServiceImpl implements VoucherService {

    @Autowired
    private VoucherMapper voucherMapper;
    @Autowired
    private VoucherTemplateService voucherTemplateService;

    @Override
    public int deleteByPrimaryKey(Integer voucherId) {
        return voucherMapper.deleteByPrimaryKey(voucherId);
    }

    @Override
    public int insert(Voucher record) {
        return voucherMapper.insert(record);
    }

    @Override
    public int insertSelective(Voucher record) {
        return voucherMapper.insertSelective(record);
    }

    @Override
    public Voucher selectByPrimaryKey(Integer voucherId) {
        return voucherMapper.selectByPrimaryKey(voucherId);
    }

    @Override
    public int updateByPrimaryKeySelective(Voucher record) {
        return voucherMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Voucher record) {
        return voucherMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Voucher> findMyValidVoucher(Integer userId, int voucherState, BigDecimal subtotal, String goodsClass, Integer storeId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId); //用户ID
        map.put("voucherState", voucherState); //是否可用
        map.put("totalAmount", subtotal); //商户内购买金额是否满足优惠券条件
        map.put("now", DateUtil.unixTimeStampInt().intValue()); //是否在优惠券优惠的时间段内
        map.put("classId", goodsClass); //是否符合商户限定的优惠商品类型
        map.put("storeId",storeId);
        List<Voucher> vouchers = voucherMapper.findMyValidVoucher(map);
        return vouchers;
    }

    @Transactional
    public String addCouponToUser(Integer voucherId, MemberBase member) {
        VoucherTpl voucherTpl = voucherTemplateService.selectByPrimaryKey(voucherId);
        Voucher voucher = new Voucher();
//        voucher.setVoucherOwnerId(member.getUserId());
//        voucher.setVoucherTId(voucherTpl.getVoucherTId());
//        voucher.setVoucherStoreId(voucherTpl.getVoucherTStoreId());
//        List<Voucher> vouchers = voucherMapper.selectByVoucherCond(voucher);
//        //判断是否已经被领完，是否已经失效，用户是否已经领取超过了规定张数
//        if(voucherTpl.getVoucherTGiveout()<voucherTpl.getVoucherTTotal() &&voucherTpl.getVoucherTState()==1&&vouchers.size()< voucherTpl.getVoucherTEachlimit()){
            voucherTpl.setVoucherTGiveout(voucherTpl.getVoucherTGiveout()+1);
            voucherTemplateService.updateByPrimaryKeySelective(voucherTpl);
            String voucherCode = SerialNumGenerator.getOrderSn(SerialNumGenerator.ORDERCH_WEBCHART).toString();
            voucher.setVoucherCode(voucherCode);
            voucher.setVoucherTId(voucherTpl.getVoucherTId());
            voucher.setVoucherTitle(voucherTpl.getVoucherTTitle());
            voucher.setVoucherDesc(voucherTpl.getVoucherTDesc());
            voucher.setVoucherStartDate(voucherTpl.getVoucherTStartDate());
            voucher.setVoucherEndDate(voucherTpl.getVoucherTEndDate());
            voucher.setVoucherPrice(voucherTpl.getVoucherTPrice());
            voucher.setVoucherLimit(voucherTpl.getVoucherTLimit());
            voucher.setVoucherStoreId(voucherTpl.getVoucherTStoreId());
            voucher.setVoucherState((byte)1);
            voucher.setVoucherActiveDate(DateUtil.toUnixTimeStampByDate(DateUtil.toTimeString(new Date())));
            voucher.setVoucherType(voucherTpl.getVoucherTScId());
            voucher.setVoucherOwnerId(member.getUserId());
            voucher.setVoucherOwnerName(member.getUserName());
            voucherMapper.insert(voucher);
//        }
        return "";
    }

    @Override
    public List<Voucher> selectByCondition(VoucherSearchBean voucherSearchBean) {
        return voucherMapper.selectByVoucherSearchBean(voucherSearchBean );
    }
}
